﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace WolTest
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] mac = new byte[6];

            //MAC地址分配到mac数组中
            mac[0] = 0xf0;
            mac[1] = 0x2f;
            mac[2] = 0x74;
            mac[3] = 0xdd;
            mac[4] = 0x98;
            mac[5] = 0x96;

            WakeUp(mac, IPAddress.Parse("192.168.1.255"));
        }

        /// <summary>
        /// 远程开机，网卡需要具备远程唤醒功能
        /// </summary>
        /// <param name="mac">网卡物理地址字符数组</param>
        public static void WakeUp(byte[] mac, IPAddress broadIp)
        {
            UdpClient client = new UdpClient();
            client.Connect(broadIp, 9090);

            byte[] packet = new byte[17 * 6];

            for (int i = 0; i < 6; i++)
                packet[i] = 0xFF;

            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];

            int result = client.Send(packet, packet.Length);
        }
    }
}
